import router from "@/router"
import store from "@/store"
import storage from "./storage"
import nprogress from "nprogress"
import 'nprogress/nprogress.css'
router.beforeEach((to,from,next) => {
    nprogress.start();
    let token = storage.get('sasa-token')
    if(token){
        if(!store.state.permission.userMenu.length){
            store.dispatch('permission/FETCH_MENU_LIST')
                .then(() => {
                    next({path:to.path,replace:true,query:to.query})
            })
        }else{
            next()
        }
    }else{
        if(to.path === '/login'){
            next()
        }else{
            next({path:"/login"})
        }
    }
})

router.afterEach((to,from) => { 
    nprogress.done();
    let matched = to.matched.filter((item,index) => index > 0)
    store.commit('permission/SET_CRUMB',matched)
})

export default router